import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		vue_token:'',
		vue_user: {}
	},
	mutations: {
		// payload为用户传递的值，可以是单一值或者对象
		changeLoginStatus(state, payload) {
			if (payload.access_token) {
				uni.setStorageSync("storeState", JSON.stringify(payload.access_token));
				state.vue_token=payload.access_token;
				
			} else {
				uni.removeStorageSync('storeState');
				state.vue_token=""
			}

		},
		changeUser(state, payload) {
			uni.setStorageSync("UserInfo", JSON.stringify({
				userInfo: payload,
			}));
			state.vue_user=payload
		},
		exit(){
			uni.removeStorageSync("UserInfo");
			uni.removeStorageSync("storeState");
		}
		
	},
	actions: {
		// 登录
		asynLogin(context, payload) {
			console.log(payload);
			context.commit('changeLoginStatus', payload)
		},
		asynUser(context, payload) {
			context.commit('changeUser', payload)
		},
		exitLogin(context, payload) {
			context.commit('exit', payload)
		},

	}
})

export default store
